import api from "../../../common/api.js";
import p from "../../../utils/platform";
import util from "../../../utils/util.js";

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    scanState: false, //是否已经扫码
    helpGuide: false, //引导弹窗
    kefu: false, //引导弹窗
    ImagesUrl: util.releaseImagesUrl,
    firstLogined: false, //pc端是否是首次登录
    navHeight: 0,
    navTop: 0,
    detainment: false, //挽留页面
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryPmReferenceDetailInfoByRefCode();
    if (options.scanCode) {
      let scanCode = options.scanCode;
      this.getAuthorization(scanCode);
    }

    this.data.firstLogined = options.firstLogined == "1";
    app.globalData.systemPromise.then(() => {
      this.setData({
        navHeight: app.globalData.navHeight,
        navTop: app.globalData.navTop,
      });
    });
  },

  handleBack() {
    // console.log("返回判断一下是否首次登录", this.data.firstLogined);

    if (this.data.firstLogined) {
      this.setData({
        detainment: true,
      });
    } else {
      p.navigateBack();
    }
  },

  closeHold() {
    this.data.firstLogined = false;
    this.setData({
      detainment: false,
    });
  },

  //扫码登录
  scanCode() {
    let that = this;
    wx.scanCode({
      success(res) {
        console.log(res);
        let token = res.result;
        that.getAuthorization(token);
      },
    });
  },
  canScanCode() {
    app
      .mobileLogin(null, "打开电脑浏览器")
      .then(() => {
        this.scanCode();
      })
      .catch(() => {
        app.globalData.afterMobileAuthorCallBack = () => {
          p.navigateBack();
        };
      });
  },
  // 验证二维码
  getAuthorization(token) {
    let that = this;
    api.getAuthorization({ token: token }, (res) => {
      // console.log(res);
      if (res.data.isSuccess) {
        // 已经成功登陆了pc端
        app.globalData.checkPcLogin = true;
        that.setData({
          scanState: true,
          firstLogined: false,
        });
      } else {
        that.setData({
          scanState: false,
        });
        wx.showToast({
          icon: "none",
          title: res.data.errorMessage,
        });
      }
    });
  },
  //复制网址
  copyLink() {
    wx.setClipboardData({
      data: "https://www.shulan.fun/#/login",
      success(res) {
        wx.getClipboardData({
          success(res) {
            console.log(res.data); // data
          },
        });
      },
    });
  },
  // 打开引导
  openHelp() {
    this.setData({
      helpGuide: true,
    });
  },
  // 关闭引导
  closeHelp() {
    this.setData({
      helpGuide: false,
    });
  },
  // 查看教程
  goCourse() {
    let url = this.data.url;
    let date = this.data.date;
    let title = "一键发房教程";
    let typename = this.data.typeName;
    this.setData({
      detainment: false,
    });

    url =
      "/pages/gatherHouse/playVideo/playVideo?date=" +
      date +
      "&url=" +
      url +
      "&title=" +
      title +
      "&typename=" +
      typename;
    
    wx.navigateTo({
      url,
    });
  },
  // 将视频连接处理
  CTE(U) {
    let url = U;
    url = url.split("/");
    let pattern = new RegExp("[\u4E00-\u9FA5]+");
    url = url.map((v) => {
      if (pattern.test(v)) {
        v = encodeURIComponent(v);
      }
      return v;
    });
    url = url.join("/");
    return url;
  },
  // 获取所有的视频资源
  queryPmReferenceDetailInfoByRefCode() {
    api.queryPmReferenceDetailInfoByRefCode(
      {
        refCode: "ProductDescription",
      },
      (res) => {
        try {
          let content = res.data.content.map((v) => {
            let refDetailValue = JSON.parse(v.refDetailValue).map((n) => {
              n.url = this.CTE(n.url);
              return n;
            });
            return {
              refDetailName: v.refDetailName,
              refDetailValue: refDetailValue,
            };
          });
  
          content.map((q) => {
            q.refDetailValue.map((w) => {
              if (w.title == "一键发房教程") {
                this.setData({
                  date: w.date,
                  typeName: w.typeName,
                  url: w.url,
                });
              }
            });
          });
        } catch (error) {
          
        }
      }
    );
  },
  // 关闭客服引导
  closekefu() {
    this.setData({
      kefu: false,
    });
  },
  // 打开客服引导
  openkefu() {
    this.setData({
      helpGuide: false,
      kefu: true,
      detainment: false,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.needShowDetainment = this.data.firstLogined
    app.globalData.lastPage = '/pages/home/scanWeb/scanWeb'
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

});
